{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# XGBoost Server Test with Iris Dataset\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import xgboost as xgb\n",
    "from sklearn.datasets import load_iris\n",
    "import os\n",
    "\n",
    "model_dir = \".\"\n",
    "BST_FILE = \"model.bst\"\n",
    "\n",
    "iris = load_iris()\n",
    "y = iris['target']\n",
    "X = iris['data']\n",
    "dtrain = xgb.DMatrix(X, label=y)\n",
    "param = {'max_depth': 6,\n",
    "            'eta': 0.1,\n",
    "            'silent': 1,\n",
    "            'nthread': 4,\n",
    "            'num_class': 10,\n",
    "            'objective': 'multi:softmax'\n",
    "            }\n",
    "xgb_model = xgb.train(params=param, dtrain=dtrain)\n",
    "model_file = os.path.join((model_dir), BST_FILE)\n",
    "xgb_model.save_model(model_file)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Wrap model using s2i"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## REST test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "s2i build -E environment_rest ./xgboostserver seldonio/seldon-core-s2i-python37:0.11-SNAPSHOT seldonio/xgboostserver_rest:0.1\n",
      "---> Installing application source...\n",
      "---> Installing dependencies ...\n",
      "Looking in links: /whl\n",
      "Collecting scikit-learn==0.20.3 (from -r requirements.txt (line 1))\n",
      "  WARNING: Url '/whl' is ignored. It is either a non-existing path or lacks a specific scheme.\n",
      "Downloading https://files.pythonhosted.org/packages/aa/cc/a84e1748a2a70d0f3e081f56cefc634f3b57013b16faa6926d3a6f0598df/scikit_learn-0.20.3-cp37-cp37m-manylinux1_x86_64.whl (5.4MB)\n",
      "Requirement already satisfied: numpy>=1.8.2 in /usr/local/lib/python3.7/site-packages (from -r requirements.txt (line 2)) (1.16.4)\n",
      "Collecting xgboost==0.82 (from -r requirements.txt (line 3))\n",
      "  WARNING: Url '/whl' is ignored. It is either a non-existing path or lacks a specific scheme.\n",
      "Downloading https://files.pythonhosted.org/packages/6a/49/7e10686647f741bd9c8918b0decdb94135b542fe372ca1100739b8529503/xgboost-0.82-py2.py3-none-manylinux1_x86_64.whl (114.0MB)\n",
      "Collecting scipy>=0.13.3 (from scikit-learn==0.20.3->-r requirements.txt (line 1))\n",
      "  WARNING: Url '/whl' is ignored. It is either a non-existing path or lacks a specific scheme.\n",
      "Downloading https://files.pythonhosted.org/packages/5d/bd/c0feba81fb60e231cf40fc8a322ed5873c90ef7711795508692b1481a4ae/scipy-1.3.0-cp37-cp37m-manylinux1_x86_64.whl (25.2MB)\n",
      "Installing collected packages: scipy, scikit-learn, xgboost\n",
      "Successfully installed scikit-learn-0.20.3 scipy-1.3.0 xgboost-0.82\n",
      "Build completed successfully\n"
     ]
    }
   ],
   "source": [
    "!cd .. && make build_rest"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8e2722f3a54cd964f6aaeb6360f38cf0517407192f01abc8ef2dc1f1fc57e706\r\n"
     ]
    }
   ],
   "source": [
    "!docker run --rm -d --name \"xgboostserver\"  -p 5000:5000 -e PREDICTIVE_UNIT_PARAMETERS='[{\"type\":\"STRING\",\"name\":\"model_uri\",\"value\":\"file:///model\"}]' -v ${PWD}:/model seldonio/xgboostserver_rest:0.1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Send some random features that conform to the contract"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "----------------------------------------\n",
      "SENDING NEW REQUEST:\n",
      "\n",
      "[[4.22  2.249 4.199 1.153]]\n",
      "RECEIVED RESPONSE:\n",
      "meta {\n",
      "}\n",
      "data {\n",
      "  ndarray {\n",
      "    values {\n",
      "      number_value: 1.0\n",
      "    }\n",
      "  }\n",
      "}\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "!seldon-core-tester contract.json 0.0.0.0 5000 -p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "xgboostserver\r\n"
     ]
    }
   ],
   "source": [
    "!docker rm xgboostserver --force"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## grpc test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "s2i build -E environment_grpc ./xgboostserver seldonio/seldon-core-s2i-python37:0.11-SNAPSHOT seldonio/xgboostserver_grpc:0.1\n",
      "---> Installing application source...\n",
      "---> Installing dependencies ...\n",
      "Looking in links: /whl\n",
      "Collecting scikit-learn==0.20.3 (from -r requirements.txt (line 1))\n",
      "  WARNING: Url '/whl' is ignored. It is either a non-existing path or lacks a specific scheme.\n",
      "Downloading https://files.pythonhosted.org/packages/aa/cc/a84e1748a2a70d0f3e081f56cefc634f3b57013b16faa6926d3a6f0598df/scikit_learn-0.20.3-cp37-cp37m-manylinux1_x86_64.whl (5.4MB)\n",
      "Requirement already satisfied: numpy>=1.8.2 in /usr/local/lib/python3.7/site-packages (from -r requirements.txt (line 2)) (1.16.4)\n",
      "Collecting xgboost==0.82 (from -r requirements.txt (line 3))\n",
      "  WARNING: Url '/whl' is ignored. It is either a non-existing path or lacks a specific scheme.\n",
      "Downloading https://files.pythonhosted.org/packages/6a/49/7e10686647f741bd9c8918b0decdb94135b542fe372ca1100739b8529503/xgboost-0.82-py2.py3-none-manylinux1_x86_64.whl (114.0MB)\n",
      "Collecting scipy>=0.13.3 (from scikit-learn==0.20.3->-r requirements.txt (line 1))\n",
      "  WARNING: Url '/whl' is ignored. It is either a non-existing path or lacks a specific scheme.\n",
      "Downloading https://files.pythonhosted.org/packages/5d/bd/c0feba81fb60e231cf40fc8a322ed5873c90ef7711795508692b1481a4ae/scipy-1.3.0-cp37-cp37m-manylinux1_x86_64.whl (25.2MB)\n",
      "Installing collected packages: scipy, scikit-learn, xgboost\n",
      "Successfully installed scikit-learn-0.20.3 scipy-1.3.0 xgboost-0.82\n",
      "Build completed successfully\n"
     ]
    }
   ],
   "source": [
    "!cd .. && make build_grpc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "147976472c29370936f7e30808373c4ae489c1c1983646327ac4cf18a362e6ac\r\n"
     ]
    }
   ],
   "source": [
    "!docker run --rm -d --name \"xgboostserver\"  -p 5000:5000 -e PREDICTIVE_UNIT_PARAMETERS='[{\"type\":\"STRING\",\"name\":\"model_uri\",\"value\":\"file:///model\"}]' -v ${PWD}:/model seldonio/xgboostserver_grpc:0.1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Test using NDArray payload"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "----------------------------------------\r\n",
      "SENDING NEW REQUEST:\r\n",
      "\r\n",
      "[[7.489 4.707 7.234 2.324]]\r\n",
      "RECEIVED RESPONSE:\r\n",
      "meta {\r\n",
      "}\r\n",
      "data {\r\n",
      "  ndarray {\r\n",
      "    values {\r\n",
      "      number_value: 2.0\r\n",
      "    }\r\n",
      "  }\r\n",
      "}\r\n",
      "\r\n",
      "\r\n"
     ]
    }
   ],
   "source": [
    "!seldon-core-tester contract.json 0.0.0.0 5000 -p --grpc"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Test using Tensor payload"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "----------------------------------------\r\n",
      "SENDING NEW REQUEST:\r\n",
      "\r\n",
      "[[7.16  2.044 7.865 2.899]]\r\n",
      "RECEIVED RESPONSE:\r\n",
      "meta {\r\n",
      "}\r\n",
      "data {\r\n",
      "  tensor {\r\n",
      "    shape: 1\r\n",
      "    values: 2.0\r\n",
      "  }\r\n",
      "}\r\n",
      "\r\n",
      "\r\n"
     ]
    }
   ],
   "source": [
    "!seldon-core-tester contract.json 0.0.0.0 5000 -p --grpc --tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "xgboostserver\r\n"
     ]
    }
   ],
   "source": [
    "!docker rm xgboostserver --force"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
